package com.tengju.user.application.archives;

import com.google.common.collect.Lists;
import com.tengju.user.application.shared.ApplicationException;
import com.tengju.user.application.shared.ApplicationExceptionCode;
import com.tengju.user.domain.model.sign.StarBlacklist;
import com.tengju.user.domain.model.sign.StarBlacklistFactory;
import com.tengju.user.domain.model.sign.StarBlacklistRepository;
import com.tengju.user.domain.model.user.StarLevelEnum;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.model.user.UserInfo;
import com.tengju.user.domain.service.UserDocService;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

@Service
@Slf4j
public class StarBlacklistService {

    private final StarBlacklistRepository starBlackListRepository;
    private final UserDocService userDocService;

    public StarBlacklistService(UserDocService userDocService,
            StarBlacklistRepository starBlackListRepository) {
        this.userDocService = userDocService;
        this.starBlackListRepository = starBlackListRepository;
    }

    public void add(Long idCode, Integer limitStarLevel, String name) {
        StarBlacklist starBlacklist = getByIdCode(idCode);
        if (starBlacklist != null) {
            throw ApplicationException
                    .error(ApplicationExceptionCode.STAR_BLACKLIST_EXIST, idCode);
        }
        UserInfo userInfo = getUserInfo(idCode);
        starBlacklist = StarBlacklistFactory
                .create(userInfo, StarLevelEnum.parseByCode(limitStarLevel), name);
        starBlackListRepository.insert(starBlacklist);
    }


    public void edit(Long idCode, Integer limitStarLevel, String name) {
        StarBlacklist starBlacklist = getByIdCode(idCode);
        if (starBlacklist == null) {
            throw ApplicationException
                    .error(ApplicationExceptionCode.STAR_BLACKLIST_NOT_EXIST, idCode);
        }
        starBlacklist.edit(limitStarLevel, name);

        starBlackListRepository.edit(starBlacklist);
    }

    public StarBlacklist getByIdCode(Long idCode) {
        UserInfo userInfo = getUserInfo(idCode);
        return starBlackListRepository.getByIdCode(idCode, userInfo.getMobile());
    }

    private UserInfo getUserInfo(Long idCode) {
        List<UserInfo> userInfos = userDocService
                .searchUserByIdCodes(Lists.newArrayList(new UserIdCode(idCode)));
        if (CollectionUtils.isEmpty(userInfos)) {
            throw ApplicationException.error(ApplicationExceptionCode.USERINFO_NOT_EXIST, idCode);
        }
        return userInfos.get(0);
    }

    public void del(Long idCode) {
        StarBlacklist starBlacklist = getByIdCode(idCode);
        if (starBlacklist == null) {
            throw ApplicationException
                    .error(ApplicationExceptionCode.STAR_BLACKLIST_NOT_EXIST, idCode);
        }
        starBlackListRepository.del(starBlacklist.getIdCode());
    }
}
